<template>
  <el-dialog v-model="visible" :title="! dataForm.institutionId ? '新增' : '修改' " :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="institutionName" label="机构名称">
        <el-input v-model="dataForm.institutionName" placeholder="机构名称"></el-input>
      </el-form-item>
      <el-form-item prop="institutionType" label="机构类型">
        <sdl-select v-model="dataForm.institutionType" clearable dict-type="institution_type"></sdl-select>
      </el-form-item>
      <el-form-item prop="institutionAddress" label="所在地">
        <el-input v-model="dataForm.institutionAddress" placeholder="所在地"></el-input>
      </el-form-item>
      <el-form-item prop="institutionPhone" label="联系电话">
        <el-input v-model="dataForm.institutionPhone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item prop="institutionMailbox" label="联系邮箱">
        <el-input v-model="dataForm.institutionMailbox" placeholder="联系邮箱"></el-input>
      </el-form-item>
      <el-form-item prop="institutionData" label="成立日期">
        <el-date-picker
          v-model="dataForm.institutionData"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="institutionCreater" label="创始人">
        <el-input v-model="dataForm.institutionCreater" placeholder="创始人"></el-input>
      </el-form-item>

      <el-form-item prop="institutionIntro" label="简介">
        <el-input type="textarea" v-model="dataForm.institutionIntro" placeholder="简介"></el-input>
      </el-form-item>
      <el-form-item prop="isDel" label="是否有效">
        <sdl-radio-group v-model="dataForm.isDel" dict-type="is_del"></sdl-radio-group>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useAppStore } from "@/store";
import { ElMessage } from "element-plus";
import SdlSelect from "@/components/sdl-select/src/sdl-select.vue";
import SdlRadioGroup from "@/components/sdl-radio-group/src/sdl-radio-group.vue";

const store = useAppStore();

const emit = defineEmits(["refreshDataList"]);
const visible = ref(false);
const dataFormRef = ref();
const loading = ref(false);

const dataForm = reactive({
  institutionId: "",
  institutionName: "",
  institutionType: "",
  institutionAddress: "",
  institutionPhone: "",
  institutionMailbox: "",
  institutionData: "",
  institutionCreater: "",
  institutionIntro: "",
  institutionProevalution: "",
  isDel: 1
});

const user = computed(() => store.state.user);

const rules = ref({
  institutionName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  institutionType: [{ required: true, message: "必填项不能为空", trigger: "change" }],
  institutionAddress: [{ required: true, message: "必填项不能为空", trigger: "change" }],
  institutionPhone: [{ required: true, message: "必填项不能为空", trigger: "change" }],
  institutionData: [{ required: true, message: "必填项不能为空", trigger: "change" }]
});

const init = (institutionId?: number) => {
  visible.value = true;
  dataForm.institutionId = "";
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (institutionId) {
    getInfo(institutionId);
  }
};
// 获取信息
const getInfo = (institutionId: number) => {
  baseService.get(`/sdl/institution/${institutionId}`).then((res) => {
    Object.assign(dataForm, res.data);
    // if (dataForm.pid === "0") {
    //   return deptListTreeSetDefaultHandle();
    // }
    // deptListTree.value.setCurrentKey(dataForm.pid);
  });
};


// 表单提交
const dataFormSubmitHandle = async () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }

    (!dataForm.institutionId ? baseService.post : baseService.put)("/sdl/institution", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>

<style lang="less" scoped>
.mod-sys__dept {
  .dept-list {
    .el-input__inner,
    .el-input__suffix {
      cursor: pointer;
    }
  }
}
</style>
